Public MustInherit Class jObjectiveControl
    Inherits WebControls.WebControl
    Implements IJObjective

    Private m_sNamespace As String
    Private m_sConstructor As String
    Private m_sDataID As String
    Private m_ctlPropertyName As New Variable

    Public Property [Namespace]() As String Implements IJObjective.Namespace
        Get
            Return m_sNamespace
        End Get
        Set(ByVal value As String)
            m_sNamespace = value
        End Set
    End Property

    Public Property ObjectName() As String Implements IJObjective.ObjectName
        Get
            Return m_ctlPropertyName.Value
        End Get
        Set(ByVal value As String)
            m_ctlPropertyName.Value = value
        End Set
    End Property

    Public Property Constructor() As String Implements IJObjective.Constructor
        Get
            Return m_sConstructor
        End Get
        Set(ByVal value As String)
            m_sConstructor = value
        End Set
    End Property

    Public Property DataID() As String Implements IJObjective.DataID
        Get
            Return m_sDataID
        End Get
        Set(ByVal value As String)
            m_sDataID = value
        End Set
    End Property

    Public Overrides ReadOnly Property ClientID() As String
        Get
            Return ID
        End Get
    End Property

    Public Sub New(Optional ByVal Tag As HtmlTextWriterTag = HtmlTextWriterTag.Div)
        MyBase.New(Tag)

        DataID = Common.GetUniqueID(Context)

        With m_ctlPropertyName
            .Name = "_this"
            .Type = Variable.DataType.String
        End With
    End Sub

    Protected Overrides Sub CreateChildControls()
        MyBase.CreateChildControls()

        ChildControlsCreated = True
    End Sub

    Private Sub ClientEnhancedControl_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        EnsureChildControls()

        If String.IsNullOrEmpty(Constructor) Then
            Constructor = "new jObjective.DataObject($('#" & DataID & "'))"
        End If
    End Sub

    Private Sub ClientEnhancedControl_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ID = DataID 'Set the ID to the hex client ID just before the postback data is loaded
    End Sub

    Private Sub ClientEnhancedControl_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
        If Not String.IsNullOrEmpty(ObjectName) Then
            With Controls
                .Add(m_ctlPropertyName)
            End With
        End If
    End Sub
End Class